<?php
/**
 * Ajax for Admin Login Class - PHP5
 *
 * Description:
 * This class handles the functionality for users login via Ajax
 *
 * @copyright	Copyright (c) eFun PMS 10th March, 2010.
 * @version 	1.0.0
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 **/
class ajax extends Controller
{
	var $c 		= 'ajax';//current controler
	var $p_c 	= 'login';//parent controller
	var $ajax_arr = array();
  
  var $ses_id   = '';
  var $cart_set	= '';
  
	/** 
	 * Welcome
	 *
	 * @desc contructor of the class
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function ajax()
	{
		parent::Controller();	
    
    $this->ajax_arr['suc'] = '';
    $this->ajax_arr['err'] = '';
    $this->ajax_arr['msg'] = '';
    $this->ajax_arr['act'] = '';
    
		//load login language
		$this->lang->load('login', ADMIN_LANGUAGE); 
    $this->ses_id = $this->session->userdata('cart_ses_id');
    if(empty($this->ses_id)){
      $this->ses_id = random_string('unique');
      $this->session->set_userdata('cart_ses_id', $this->ses_id);
    }
	}
	
	/* ------------------------------------------------------------------------- */
	
	/** 
	 * Index
	 *
	 * @desc NA
	 * @access public/private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function index()
	{		
		exit;
	}
  
  
  //------------------------------------------------------------------
  
  /** 
	 * AddA - Add Article
	 *
	 * @desc NA
	 * @access public
	 * @param  string
   * @param  inr	 
	 * @return void
	 */
	function adda()
	{ 
    $info = array();
    $info['id'] = (int) $this->input->post('id');
    $info['qty'] = (int) 1;//$this->input-post('qty');
    $info['type'] = 'article';
    
    
    $qry = "SELECT a.os_articles_id as `id`, a.os_articles_title as `name`, 
                  a.os_journals_id as `jid`, a.os_articles_price as `price`
            FROM ". TBL_ARTICLES ."  a 
            WHERE a.os_articles_id = '".$info['id']."'";    
    $set = run_cust_qry($qry,TRUE);
    
    if(!empty($info['id']) && $set['num_rows'] > 0){
      $info['name'] = $set['list']['name'];
      $info['url']  = site_url('/journals/contents_list/'.$set['list']['jid']);
      $info['price'] = $set['list']['price'];
      $key = 'article_'.$info['id'];    
      $this->_update_cart($key,$info);
            
      $this->ajax_arr['suc'] = 1;
      $this->ajax_arr['msg'] = "Article: <b>".$set['list']['name']."</b>, added successfuly to your shopping bag.<br/>
                                <a href='".site_url('cart')."' title='Click here to view Shopping Cart'>&raquo;Shopping Cart</a> ";
    }else {
      $this->ajax_arr['err'] = 1;
      $this->ajax_arr['msg'] = "Sorry the article could not be added to shopping bag.";
    }
    
    $this->_ajax_wrapper();
	}
  
  //------------------------------------------------------------------
  
  /** 
	 * AddA - Add Article
	 *
	 * @desc NA
	 * @access public
	 * @param  string
   * @param  inr	 
	 * @return void
	 */
	function dela()
	{ 
    $info = array();
    $id = (int) $this->input->post('id');
    $key = 'article_'.$id;
    if($this->_check_product($key)){
      
      $this->_delele_product($key);
      $this->ajax_arr['suc'] = 1;
      $this->ajax_arr['msg'] = "Article is deleted from your shopping bag.<br/>
                                <a href='".site_url('cart')."' title='Click here to view Shopping Cart'>&raquo;Shopping Cart</a> ";
      $this->ajax_arr['act'] = ('cart'); 
    }else {
      $this->ajax_arr['err'] = 1;
      $this->ajax_arr['msg'] = "Sorry the article could not be deleted from your shopping bag.";
      $this->ajax_arr['act'] = ('cart'); 
    }
    
    $this->_ajax_wrapper();
	}
  
  //------------------------------------------------------------------
  
  /** 
	 * Update Cart
	 *
	 * @desc NA
	 * @access provate
	 * @param  string
   * @param  array	 
	 * @return void
	 */
  function _update_cart($key,$info){
    /*set the current session data*/
    $cart = array();
    $cart = $this->session->userdata('cart');
    
    /*overite or add new data*/
    $cart[$this->ses_id][$key] = $info;
    $this->session->set_userdata('cart',$cart);
  }
  
  
  //------------------------------------------------------------------
  
  /** 
	 * Update Cart
	 *
	 * @desc NA
	 * @access provate
	 * @param  string
   * @param  array	 
	 * @return void
	 */
  function shopcart(){
    /*set the current session data*/
    $cart = array();
    $cart = $this->session->userdata('cart');
    
    /*overite or add new data*/
    echo count($cart[$this->ses_id]);
  }
  
  //------------------------------------------------------------------
  
  /** 
	 * Check Product
	 *
	 * @desc NA
	 * @access provate
	 * @param  string
	 * @return void
	 */
  function _check_product($key){
    /*set the current session data*/
    $cart = array();
    $cart = $this->session->userdata('cart');    
    if(isset($cart[$this->ses_id][$key])){
      return TRUE;
    }  else {
      return FALSE;
    }
  }
  
  //------------------------------------------------------------------
  
  /** 
	 * Update Cart
	 *
	 * @desc NA
	 * @access provate
	 * @param  string
   * @param  array	 
	 * @return void
	 */
  function _delele_product($key){
    /*set the current session data*/
    $cart = array();
    $cart = $this->session->userdata('cart');
    
    /*remove data*/
    if(isset($cart[$this->ses_id][$key])){
      unset($cart[$this->ses_id][$key]);
      $this->session->set_userdata('cart',$cart);
    }
    
  }
  
	/** 
	 * Ajax Wrapper
	 *
	 * @descp This will send the arry in JSON format
	 * @access private
	 * @param  NA	 
	 * @return void
	 */
	function _ajax_wrapper()
	{		
		echo json_encode($this->ajax_arr);
	}
	
}

/* End of file welcome.php */
/* Location: ./system/application/modules/admin_login/controllers/ajax.php */
?>